package com.chaihu.front.service.login.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chaihu.bean.enums.MsgCode;
import com.chaihu.bean.result.ResponseResult;
import com.chaihu.core.base.service.front.BaseComponentService;
import com.chaihu.core.cache.MappingCache;
import com.chaihu.core.context.IPageData;
import com.chaihu.core.factory.AuthenticationFactory;
import com.chaihu.front.service.login.LoginService;
import com.chaihu.utils.constant.MappingConstant;
import com.chaihu.utils.constant.ServiceCodeConstant;
import com.chaihu.utils.constant.ServiceConstant;
import com.chaihu.utils.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 登录信息实现类
 * Created by wuxw on 2019/3/20.
 */

@Slf4j
@Service("loginServiceImpl")
public class LoginServiceImpl extends BaseComponentService implements LoginService {

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public ResponseEntity<String> doLoginPc(IPageData pd) {
        ResponseEntity<String> responseEntity = null;
        JSONObject loginInfo = JSONObject.parseObject(pd.getReqData());
        String apiUrl = null;
        if (loginInfo.containsKey("mobile") && StringUtils.isNotEmpty(loginInfo.getString("mobile"))) {
            // 手机号验证码登录
            apiUrl = ServiceConstant.SERVICE_API_URL + "/api/" + ServiceCodeConstant.SERVICE_CODE_USER_SERVICE_PC_SMS_LOGIN;
            responseEntity = validateSmsValidationCode(pd);
            if (responseEntity != null) {
                return responseEntity;
            }
        } else {
            // 账号密码登录
            apiUrl = ServiceConstant.SERVICE_API_URL + "/api/" + ServiceCodeConstant.SERVICE_CODE_USER_SERVICE_PC_ACCOUNT_LOGIN;
            Assert.jsonObjectHaveKey(pd.getReqData(), "account", "请求报文格式错误或未包含账号信息");

            //调用 验证码组件验证码是否正确
            responseEntity = this.invokeComponent("validate-code", "validate", pd);
            if (responseEntity.getStatusCode() != HttpStatus.OK) {
                return responseEntity;
            }
            loginInfo.put("passwd", AuthenticationFactory.passwdMd5(loginInfo.getString("passwd")));
        }

        responseEntity = this.callCenterService(restTemplate, pd, loginInfo.toJSONString(), apiUrl, HttpMethod.POST);
        if (responseEntity.getStatusCode() == HttpStatus.OK) {
            pd.setToken(JSONObject.parseObject(responseEntity.getBody()).getString("token"));
        }
        return responseEntity;
    }

    private ResponseEntity validateSmsValidationCode(IPageData pd) {
        JSONObject param = JSONObject.parseObject(pd.getReqData());
        String messageCode = param.getString("messageCode");

        if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_UNIVERSAL_VALIDATION_CODE_ON_OFF)) && StringUtils.equals(MappingCache.getValue(MappingConstant.KEY_UNIVERSAL_VALIDATION_CODE), messageCode)) {
            // 跳过验证码校验
            return null;
        }

        // 验证码校验
        ResponseEntity responseEntity = this.invokeComponent("validate-tel", "validate", pd);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            ResponseResult result = new ResponseResult(MsgCode.ERROR.getCode(), String.valueOf(responseEntity.getBody()));
            return new ResponseEntity<String>(JSON.toJSONString(result), HttpStatus.OK);
        }

        return null;
    }

}
